/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { ClassroomVo } from "~/classroom/api/classroom.ts";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";
import { useMessage } from "@/hooks/useMessage.ts";
import { deleteByIds } from "~/classroom/api/classroom";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { ElTag } from "element-plus";
import { UserVo } from "@/modules/base/api/user";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any
): MaProTableColumns[] {
  const dictStore = useDictStore();
  const msg = useMessage();

  const showBtn = (auth: string | string[], row: ClassroomVo) => {
    return hasAuth(auth);
  };
  // name: 班级名称
  // school_id: 所属学校
  // teacher_id: 责任教师
  // department_id: 所属院系
  // member_count: 班级人数
  // creator_id: 创建者
  // creator: 创建者
  return [
    // 多选列
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
      selectable: (row: ClassroomVo) => ![1].includes(row.id as number),
    },
    // 索引序号列
    { type: "index" },
    // 普通列

    { label: () => t("classroom.name"), prop: "name" },
    { label: () => t("classroom.school"), prop: "school.name" },
    { label: () => t("classroom.department"), prop: "department.name" },
    {
      label: () => t("classroom.teacher"),
      prop: "teacher.nickname",
      cellRender: ({ row }) => <ElTag>{row.teacher.nickname}</ElTag>,
      width: 100,
    },
    {
      label: () => t("classroom.teachers"),
      prop: "teachers",
      cellRender: ({ row }) => {
        const teachers = row.teachers as UserVo[];
        console.log(teachers);
        if (!(teachers?.length > 0)) {
          return <>无</>;
        }
        return (
          <>
            {teachers.map(({ nickname }) => (
              <ElTag type="warning">{nickname}</ElTag>
            ))}
          </>
        );
      },
    },
    { label: () => t("classroom.member_count"), prop: "member_count" },
    { label: () => t("common.created_at"), prop: "created_at" },
    { label: () => t("common.updated_at"), prop: "updated_at" },
    { label: () => t("common.creator"), prop: "creator.nickname" },
    {
      type: "operation",
      fixed: "right",
      label: () => t("crud.operation"),
      operationConfigure: {
        actions: [
          {
            name: "member",
            icon: "ep:user-filled",
            show: ({ row }) => showBtn("classroom:member", row),
            text: () => t("classroom.member"),
            onClick: ({ row }) => {
              console.log(row);
            },
          },
          {
            name: "assign-teacher",
            icon: "el:group",
            show: ({ row }) => showBtn("classroom:assign-teacher", row),
            text: () => t("classroom.assign-teacher"),
            onClick: ({ row }) => {
              dialog.setTitle(row.name);
              dialog.open({ formType: "assignTeacher", data: row });
            },
          },
          {
            name: "achievement-stat",
            icon: "material-symbols:line-axis",
            show: ({ row }) => showBtn("classroom:achievement-stat", row),
            text: () => t("classroom.achievement-stat"),
            onClick: ({ row }) => {
              console.log(row);
            },
          },
          {
            name: "live-script-summary",
            icon: "material-symbols:summarize",
            show: ({ row }) => showBtn("classroom:live-script-summary", row),
            text: () => t("classroom.live-script-summary"),
            onClick: ({ row }) => {
              console.log(row);
            },
          },
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) => showBtn("classroom:update", row),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            show: ({ row }) => showBtn("classroom:delete", row),
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
